package com.modular.result;

import com.modular.constant.BaseCodeDesc;
import com.modular.exception.CodeDesc;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

@Setter
@Getter
@ToString
public class RspMessage<T> implements Serializable {
    private String code;
    private String errMsg;
    private T data;

    public RspMessage(String code,String errMsg){
        this.code = code;
        this.errMsg = errMsg;
    }
    public RspMessage(String code,T data){
        this.code = code;
        this.data = data;
    }

    public RspMessage(String code,String errMsg,T data){
        this.code = code;
        this.errMsg = errMsg;
        this.data = data;
    }

    public static <T> RspMessage<T> success() {
        return result(BaseCodeDesc.SUCCESS);
    }
    public static <T> RspMessage<T> success(T data) {
        return result(BaseCodeDesc.SUCCESS, data);
    }
    public static <E> RspMessage<E> result(CodeDesc codeDesc) {
        return new RspMessage(codeDesc.getCode(),codeDesc.getErrMsg());
    }
    public static <E> RspMessage<E> result(CodeDesc codeDesc, E data) {
        return new RspMessage(codeDesc.getCode(),codeDesc.getErrMsg(), data);
    }
    public static <E> RspMessage<E> result(String code,String errMsg, E data) {
        return new RspMessage(code,errMsg, data);
    }
}
